package com.fr.chart.plot;

import com.fr.base.StringUtils;
import com.fr.base.core.GraphHelper;
import com.fr.base.core.html.Tag;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLReadable;
import com.fr.base.xml.XMLableReader;
import com.fr.chart.core.Glyph;
import com.fr.chart.core.glyph.ChartGlyph;
import com.fr.chart.core.glyph.PlotGlyph;
import com.fr.chart.core.glyph.ShapeGlyph;
import com.fr.chart.core.glyph.SoloGlyph;
import com.fr.chart.core.glyph.TextGlyph;
import com.fr.chart.shape3d.Cubic;
import com.fr.report.parameter.Parameter;
import com.fr.report.script.Calculator;
import com.fr.report.script.ParameterMapNameSpace;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.PathIterator;
import java.awt.geom.Rectangle2D;

public class DataPoint extends SoloGlyph
{
  private static final long serialVersionUID = -970056669601457244L;
  public static final String XML_TAG = "DataPoint";
  private int categoryIndex;
  private double value;
  private boolean valueIsNull = false;
  private String categoryName;
  private String seriesName;
  private double percentValue;
  private String seriesLabel = null;
  private Glyph drawImpl;
  private TextGlyph dataLabel;
  private GeneralPath leadLine;

  public DataPoint()
  {
  }

  public DataPoint(int paramInt, String paramString1, String paramString2, double paramDouble)
  {
    this.categoryIndex = paramInt;
    this.categoryName = paramString1;
    this.seriesName = paramString2;
    this.value = paramDouble;
  }

  public void setIndex(int paramInt)
  {
    this.categoryIndex = paramInt;
  }

  public int getIndex()
  {
    return this.categoryIndex;
  }

  public void setValue(double paramDouble)
  {
    this.value = paramDouble;
  }

  public double getValue()
  {
    return this.value;
  }

  public void setValueIsNull(boolean paramBoolean)
  {
    this.valueIsNull = paramBoolean;
  }

  public boolean isValueIsNull()
  {
    return this.valueIsNull;
  }

  public void setPercentValue(double paramDouble)
  {
    this.percentValue = paramDouble;
  }

  public double getPercentValue()
  {
    return this.percentValue;
  }

  public void setCategoryName(String paramString)
  {
    this.categoryName = paramString;
  }

  public String getCategoryName()
  {
    return this.categoryName;
  }

  public void setSeriesName(String paramString)
  {
    this.seriesName = paramString;
  }

  public String getSeriesName()
  {
    return this.seriesName;
  }

  public void setSereisLabel(String paramString)
  {
    this.seriesLabel = paramString;
  }

  public String getSereisLabel()
  {
    return this.seriesLabel;
  }

  public Calculator getCalculator()
  {
    Calculator localCalculator = Calculator.createCalculator();
    localCalculator.pushNameSpace(ParameterMapNameSpace.create(new Parameter[] { new Parameter("CATEGORY", getCategoryName()), new Parameter("SERIES", getSeriesName()), new Parameter("VALUE", new Double(this.value)), new Parameter("PERCENT", new Double(getPercentValue())) }));
    return localCalculator;
  }

  public void setDrawImpl(Glyph paramGlyph)
  {
    this.drawImpl = paramGlyph;
  }

  public Glyph getDrawImpl()
  {
    return this.drawImpl;
  }

  public void setDataLabel(TextGlyph paramTextGlyph)
  {
    this.dataLabel = paramTextGlyph;
  }

  public TextGlyph getDataLabel()
  {
    return this.dataLabel;
  }

  public void setLeadLine(GeneralPath paramGeneralPath)
  {
    this.leadLine = paramGeneralPath;
  }

  public GeneralPath getLeadLine()
  {
    return this.leadLine;
  }

  public void setShape(Shape paramShape)
  {
    this.drawImpl = new ShapeGlyph(paramShape);
  }

  public Shape getShape()
  {
    return ((this.drawImpl == null) ? null : this.drawImpl.getShape());
  }

  public void draw(Graphics paramGraphics)
  {
    if (this.drawImpl != null)
      this.drawImpl.draw(paramGraphics);
  }

  public void drawLabel(Graphics paramGraphics)
  {
    if (this.valueIsNull)
      return;
    if (this.dataLabel != null)
      this.dataLabel.draw(paramGraphics);
    if (getLeadLine() != null)
    {
      Color localColor = paramGraphics.getColor();
      paramGraphics.setColor(Color.gray);
      GraphHelper.draw(paramGraphics, getLeadLine(), 1);
      paramGraphics.setColor(localColor);
    }
  }

  public Tag getImageMapAreaTag(ChartGlyph paramChartGlyph)
  {
    if ((paramChartGlyph == null) || (paramChartGlyph.getPlotGlyph() == null) || (paramChartGlyph.getPlotGlyph().getBounds() == null) || (isValueIsNull()))
      return new Tag();
    int i = (int)paramChartGlyph.getPlotGlyph().getBounds().getX();
    int j = (int)paramChartGlyph.getPlotGlyph().getBounds().getY();
    Tag localTag = new Tag("area");
    localTag.attr("shape", getShapeType());
    localTag.attr("coords", getShapeCoords(i, j));
    localTag.attr("alt", "");
    localTag.attr("href", "javascript:void(0)");
    return localTag;
  }

  private String getShapeType()
  {
    if (getShape() instanceof Rectangle2D)
      return "rect";
    return "poly";
  }

  private String getShapeCoords(int paramInt1, int paramInt2)
  {
    if (getShape() instanceof Rectangle2D)
      return getRectCoords((Rectangle2D)getShape(), paramInt1, paramInt2);
    return getPolyCoords(getShape(), paramInt1, paramInt2);
  }

  private String getRectCoords(Rectangle2D paramRectangle2D, int paramInt1, int paramInt2)
  {
    if (paramRectangle2D == null)
      throw new IllegalArgumentException("Null 'rectangle' argument.");
    int i = (int)paramRectangle2D.getX() + paramInt1;
    int j = (int)paramRectangle2D.getY() + paramInt2;
    int k = i + (int)paramRectangle2D.getWidth();
    int l = j + (int)paramRectangle2D.getHeight();
    if (k == i)
      ++k;
    if (l == j)
      ++l;
    return i + "," + j + "," + k + "," + l;
  }

  private String getPolyCoords(Shape paramShape, int paramInt1, int paramInt2)
  {
    if (paramShape == null)
      throw new IllegalArgumentException("Null 'shape' argument.");
    StringBuffer localStringBuffer = new StringBuffer();
    int i = 1;
    float[] arrayOfFloat = new float[6];
    PathIterator localPathIterator = paramShape.getPathIterator(null, 1.0D);
    while (!(localPathIterator.isDone()))
    {
      localPathIterator.currentSegment(arrayOfFloat);
      if (i != 0)
      {
        i = 0;
        localStringBuffer.append((int)arrayOfFloat[0] + paramInt1);
        localStringBuffer.append(",").append((int)arrayOfFloat[1] + paramInt2);
      }
      else
      {
        localStringBuffer.append(",");
        localStringBuffer.append((int)arrayOfFloat[0] + paramInt1);
        localStringBuffer.append(",");
        localStringBuffer.append((int)arrayOfFloat[1] + paramInt2);
      }
      localPathIterator.next();
    }
    return localStringBuffer.toString();
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof DataPoint))
      return false;
    DataPoint localDataPoint = (DataPoint)paramObject;
    if (this.categoryName == null)
      if (localDataPoint.categoryName == null);
    else if (!(this.categoryName.equals(localDataPoint.categoryName)))
      return false;
    if (this.seriesName == null)
      if (localDataPoint.seriesName == null);
    else if (!(this.seriesName.equals(localDataPoint.seriesName)))
      return false;
    return (this.value == localDataPoint.value);
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    DataPoint localDataPoint = (DataPoint)super.clone();
    return localDataPoint;
  }

  public String toString()
  {
    return this.categoryName.toString() + " " + this.seriesName.toString() + " " + getValue();
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    if (paramXMLableReader.isChildNode())
    {
      String str2;
      String str1 = paramXMLableReader.getTagName();
      if (str1.equals("Attr"))
      {
        if ((str2 = paramXMLableReader.getAttr("valueIsNull")) != null)
          this.valueIsNull = Boolean.valueOf(str2).booleanValue();
        if ((str2 = paramXMLableReader.getAttr("index")) != null)
          setIndex(Integer.parseInt(str2));
        if ((str2 = paramXMLableReader.getAttr("value")) != null)
          setValue(Double.valueOf(str2).doubleValue());
        if ((str2 = paramXMLableReader.getAttr("categoryName")) != null)
          setCategoryName(str2);
        if ((str2 = paramXMLableReader.getAttr("seriesName")) != null)
          setSeriesName(str2);
        if ((str2 = paramXMLableReader.getAttr("percent")) != null)
          setPercentValue(Double.valueOf(str2).doubleValue());
        if ((str2 = paramXMLableReader.getAttr("seriesLabel")) != null)
          setSereisLabel(str2);
      }
      if (str1.equals("DrawImpl"))
        paramXMLableReader.readXMLObject(new XMLReadable(this)
        {
          private final DataPoint this$0;

          public void readXML()
          {
            String str = paramXMLableReader.getTagName();
            if (str.equals("ShapeGlyph"))
              DataPoint.access$002(this.this$0, (Glyph)paramXMLableReader.readXMLObject(new ShapeGlyph()));
            else if (str.equals("Cubic"))
              DataPoint.access$002(this.this$0, (Glyph)paramXMLableReader.readXMLObject(new Cubic()));
            else if (str.equals("MarkerGlyph"))
              DataPoint.access$002(this.this$0, (Glyph)paramXMLableReader.readXMLObject(new MarkerGlyph()));
            else if (str.equals("Meter"))
              DataPoint.access$002(this.this$0, (Glyph)paramXMLableReader.readXMLObject(new Meter()));
          }
        });
      if (str1.equals("DataLabel"))
        paramXMLableReader.readXMLObject(new XMLReadable(this)
        {
          private final DataPoint this$0;

          public void readXML()
          {
            String str = paramXMLableReader.getTagName();
            if (str.equals("TextGlyph"))
              this.this$0.setDataLabel((TextGlyph)paramXMLableReader.readXMLObject(new TextGlyph()));
          }
        });
      if (str1.equals("Line"))
      {
        double d1 = 0.0D;
        double d2 = 0.0D;
        double d3 = 0.0D;
        double d4 = 0.0D;
        double d5 = 0.0D;
        double d6 = 0.0D;
        if ((str2 = paramXMLableReader.getAttr("x1")) != null)
          d1 = Double.valueOf(str2).doubleValue();
        if ((str2 = paramXMLableReader.getAttr("y1")) != null)
          d3 = Double.valueOf(str2).doubleValue();
        if ((str2 = paramXMLableReader.getAttr("x2")) != null)
          d2 = Double.valueOf(str2).doubleValue();
        if ((str2 = paramXMLableReader.getAttr("y2")) != null)
          d4 = Double.valueOf(str2).doubleValue();
        if ((str2 = paramXMLableReader.getAttr("x3")) != null)
          d5 = Double.valueOf(str2).doubleValue();
        else
          d5 = d2;
        if ((str2 = paramXMLableReader.getAttr("y3")) != null)
          d6 = Double.valueOf(str2).doubleValue();
        else
          d6 = d4;
        this.leadLine = new GeneralPath();
        this.leadLine.moveTo((float)d1, (float)d3);
        this.leadLine.lineTo((float)d2, (float)d4);
        this.leadLine.lineTo((float)d5, (float)d6);
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    paramXMLPrintWriter.startTAG("DataPoint");
    paramXMLPrintWriter.startTAG("Attr").attr("valueIsNull", this.valueIsNull).attr("index", getIndex()).attr("value", getValue()).attr("categoryName", getCategoryName()).attr("seriesName", getSeriesName()).attr("percent", getPercentValue());
    if (StringUtils.isNotBlank(this.seriesLabel))
      paramXMLPrintWriter.attr("seriesLabel", this.seriesLabel);
    paramXMLPrintWriter.end();
    if (this.drawImpl != null)
    {
      paramXMLPrintWriter.startTAG("DrawImpl");
      this.drawImpl.writeXML(paramXMLPrintWriter);
      paramXMLPrintWriter.end();
    }
    if (this.dataLabel != null)
    {
      paramXMLPrintWriter.startTAG("DataLabel");
      this.dataLabel.writeXML(paramXMLPrintWriter);
      paramXMLPrintWriter.end();
    }
    if (this.leadLine != null)
    {
      AffineTransform localAffineTransform = new AffineTransform();
      PathIterator localPathIterator = this.leadLine.getPathIterator(localAffineTransform);
      double[][] arrayOfDouble = new double[3][2];
      int i = 0;
      while (!(localPathIterator.isDone()))
      {
        localPathIterator.currentSegment(arrayOfDouble[i]);
        ++i;
        localPathIterator.next();
      }
      paramXMLPrintWriter.startTAG("Line").attr("x1", arrayOfDouble[0][0]).attr("y1", arrayOfDouble[0][1]).attr("x2", arrayOfDouble[1][0]).attr("y2", arrayOfDouble[1][1]).attr("x3", arrayOfDouble[2][0]).attr("y3", arrayOfDouble[2][1]).end();
    }
    paramXMLPrintWriter.end();
  }

  public JSONObject toJSONObject()
    throws JSONException
  {
    JSONObject localJSONObject = super.toJSONObject();
    localJSONObject.put("categoryIndex", this.categoryIndex);
    localJSONObject.put("categoryName", this.categoryName);
    localJSONObject.put("value", this.value);
    localJSONObject.put("valueIsNull", this.valueIsNull);
    localJSONObject.put("seriesName", this.seriesName);
    localJSONObject.put("percentValue", this.percentValue);
    localJSONObject.put("seriesLabel", this.seriesLabel);
    if (this.dataLabel != null)
      localJSONObject.put("dataLabel", this.dataLabel.toJSONObject());
    return localJSONObject;
  }

  static Glyph access$002(DataPoint paramDataPoint, Glyph paramGlyph)
  {
    return (paramDataPoint.drawImpl = paramGlyph);
  }
}